{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gender Classification Of Names\n",
    "### Using Machine Learning To Detect/Predict Gender of Individuals \n",
    "+ Sklearn\n",
    "+ Pandas\n",
    "+ Text Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# EDA packages\n",
    "import pandas as pd\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ML Packages\n",
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "from sklearn.feature_extraction import DictVectorizer\n",
    "#from sklearn.feature_extraction.text import TfidfVectorizer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load our data\n",
    "df = pd.read_csv('names_dataset.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>name</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>Mary</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>Anna</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>Emma</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>Elizabeth</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Minnie</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   index       name sex\n",
       "0      0       Mary   F\n",
       "1      1       Anna   F\n",
       "2      2       Emma   F\n",
       "3      3  Elizabeth   F\n",
       "4      4     Minnie   F"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "285075"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['index', 'name', 'sex'], dtype='object')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Data Cleaning\n",
    "# Checking for column name consistency\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "index     int64\n",
       "name     object\n",
       "sex      object\n",
       "dtype: object"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Data Types\n",
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "index    0\n",
       "name     0\n",
       "sex      0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Checking for Missing Values\n",
    "df.isnull().isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "181800"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Number of Female Names\n",
    "df[df.sex == 'F'].size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "103275"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Number of Male Names\n",
    "df[df.sex == 'M'].size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_names = df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Replacing All F and M with 0 and 1 respectively\n",
    "df_names.sex.replace({'F':0,'M':1},inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1], dtype=int64)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_names.sex.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "index     int64\n",
       "name     object\n",
       "sex       int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_names.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "Xfeatures =df_names['name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Feature Extraction \n",
    "cv = CountVectorizer()\n",
    "X = cv.fit_transform(Xfeatures)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['aaban',\n",
       " 'aabha',\n",
       " 'aabid',\n",
       " 'aabriella',\n",
       " 'aada',\n",
       " 'aadam',\n",
       " 'aadan',\n",
       " 'aadarsh',\n",
       " 'aaden',\n",
       " 'aadesh',\n",
       " 'aadhav',\n",
       " 'aadhavan',\n",
       " 'aadhi',\n",
       " 'aadhira',\n",
       " 'aadhvik',\n",
       " 'aadhya',\n",
       " 'aadhyan',\n",
       " 'aadi',\n",
       " 'aadian',\n",
       " 'aadil',\n",
       " 'aadin',\n",
       " 'aadish',\n",
       " 'aadison',\n",
       " 'aadit',\n",
       " 'aadith',\n",
       " 'aadithya',\n",
       " 'aaditri',\n",
       " 'aaditya',\n",
       " 'aadiv',\n",
       " 'aadon',\n",
       " 'aadrian',\n",
       " 'aadrika',\n",
       " 'aadrit',\n",
       " 'aadvik',\n",
       " 'aadvika',\n",
       " 'aadya',\n",
       " 'aadyn',\n",
       " 'aafia',\n",
       " 'aafreen',\n",
       " 'aagam',\n",
       " 'aage',\n",
       " 'aagot',\n",
       " 'aahaan',\n",
       " 'aahan',\n",
       " 'aahana',\n",
       " 'aahil',\n",
       " 'aahir',\n",
       " 'aahliyah',\n",
       " 'aahna',\n",
       " 'aahron',\n",
       " 'aaidan',\n",
       " 'aaiden',\n",
       " 'aaidyn',\n",
       " 'aaila',\n",
       " 'aailiyah',\n",
       " 'aailyah',\n",
       " 'aaima',\n",
       " 'aaira',\n",
       " 'aairah',\n",
       " 'aaisha',\n",
       " 'aaishah',\n",
       " 'aaiyana',\n",
       " 'aaiza',\n",
       " 'aaja',\n",
       " 'aajah',\n",
       " 'aajaylah',\n",
       " 'aajon',\n",
       " 'aakanksha',\n",
       " 'aakarsh',\n",
       " 'aakash',\n",
       " 'aakeem',\n",
       " 'aakilah',\n",
       " 'aakira',\n",
       " 'aakiyah',\n",
       " 'aakriti',\n",
       " 'aala',\n",
       " 'aalaiya',\n",
       " 'aalaiyah',\n",
       " 'aalana',\n",
       " 'aalanah',\n",
       " 'aalani',\n",
       " 'aalap',\n",
       " 'aalaya',\n",
       " 'aalayah',\n",
       " 'aalayiah',\n",
       " 'aalayjah',\n",
       " 'aalayna',\n",
       " 'aalaysha',\n",
       " 'aalaysia',\n",
       " 'aalea',\n",
       " 'aaleah',\n",
       " 'aaleahya',\n",
       " 'aaleena',\n",
       " 'aaleeya',\n",
       " 'aaleeyah',\n",
       " 'aaleiah',\n",
       " 'aaleigha',\n",
       " 'aaleiyah',\n",
       " 'aalena',\n",
       " 'aalexis',\n",
       " 'aalexus',\n",
       " 'aaleya',\n",
       " 'aaleyah',\n",
       " 'aali',\n",
       " 'aalia',\n",
       " 'aaliah',\n",
       " 'aaliana',\n",
       " 'aalias',\n",
       " 'aaliayah',\n",
       " 'aaliayh',\n",
       " 'aalicia',\n",
       " 'aaliha',\n",
       " 'aalijah',\n",
       " 'aalim',\n",
       " 'aalimah',\n",
       " 'aalina',\n",
       " 'aalinah',\n",
       " 'aalisa',\n",
       " 'aalisha',\n",
       " 'aalivia',\n",
       " 'aaliya',\n",
       " 'aaliyaa',\n",
       " 'aaliyah',\n",
       " 'aaliyaha',\n",
       " 'aaliyahmarie',\n",
       " 'aaliyahrose',\n",
       " 'aaliyan',\n",
       " 'aaliyana',\n",
       " 'aaliyanna',\n",
       " 'aaliyha',\n",
       " 'aaliyiah',\n",
       " 'aalliyah',\n",
       " 'aallyah',\n",
       " 'aalok',\n",
       " 'aalon',\n",
       " 'aalya',\n",
       " 'aalyah',\n",
       " 'aalycia',\n",
       " 'aalyha',\n",
       " 'aalyia',\n",
       " 'aalyiah',\n",
       " 'aalyna',\n",
       " 'aalysia',\n",
       " 'aalyssa',\n",
       " 'aalyvia',\n",
       " 'aamanda',\n",
       " 'aamanee',\n",
       " 'aamani',\n",
       " 'aamar',\n",
       " 'aamari',\n",
       " 'aamarion',\n",
       " 'aamaya',\n",
       " 'aamber',\n",
       " 'aamena',\n",
       " 'aamer',\n",
       " 'aamia',\n",
       " 'aamil',\n",
       " 'aamilah',\n",
       " 'aamina',\n",
       " 'aaminah',\n",
       " 'aamir',\n",
       " 'aamira',\n",
       " 'aamirah',\n",
       " 'aamiya',\n",
       " 'aamiyah',\n",
       " 'aamna',\n",
       " 'aamoni',\n",
       " 'aamori',\n",
       " 'aamya',\n",
       " 'aamyah',\n",
       " 'aana',\n",
       " 'aanand',\n",
       " 'aanav',\n",
       " 'aanaya',\n",
       " 'aanchal',\n",
       " 'aania',\n",
       " 'aaniah',\n",
       " 'aanijah',\n",
       " 'aanika',\n",
       " 'aanisah',\n",
       " 'aaniya',\n",
       " 'aaniyah',\n",
       " 'aaniylah',\n",
       " 'aansh',\n",
       " 'aanshi',\n",
       " 'aanvi',\n",
       " 'aanya',\n",
       " 'aanyah',\n",
       " 'aanyla',\n",
       " 'aapri',\n",
       " 'aaqib',\n",
       " 'aaqil',\n",
       " 'aara',\n",
       " 'aarabella',\n",
       " 'aarabhi',\n",
       " 'aaradhana',\n",
       " 'aaradhy',\n",
       " 'aaradhya',\n",
       " 'aaraiz',\n",
       " 'aaralyn',\n",
       " 'aaralynn',\n",
       " 'aaran',\n",
       " 'aaraon',\n",
       " 'aaratrika',\n",
       " 'aarav',\n",
       " 'aaravi',\n",
       " 'aaraya',\n",
       " 'aaren',\n",
       " 'aareon',\n",
       " 'aareona',\n",
       " 'aari',\n",
       " 'aaria',\n",
       " 'aariah',\n",
       " 'aarian',\n",
       " 'aariana',\n",
       " 'aarianna',\n",
       " 'aarib',\n",
       " 'aaric',\n",
       " 'aarica',\n",
       " 'aarick',\n",
       " 'aarie',\n",
       " 'aariel',\n",
       " 'aarielle',\n",
       " 'aarien',\n",
       " 'aarik',\n",
       " 'aarika',\n",
       " 'aariketh',\n",
       " 'aarilyn',\n",
       " 'aarilynn',\n",
       " 'aarin',\n",
       " 'aarini',\n",
       " 'aarion',\n",
       " 'aariona',\n",
       " 'aarionna',\n",
       " 'aaris',\n",
       " 'aarish',\n",
       " 'aarit',\n",
       " 'aariv',\n",
       " 'aariya',\n",
       " 'aariyah',\n",
       " 'aariyan',\n",
       " 'aariyana',\n",
       " 'aariyanna',\n",
       " 'aariyona',\n",
       " 'aariyonna',\n",
       " 'aariz',\n",
       " 'aarjav',\n",
       " 'aarn',\n",
       " 'aarna',\n",
       " 'aarnav',\n",
       " 'aarnavi',\n",
       " 'aarne',\n",
       " 'aaro',\n",
       " 'aaroh',\n",
       " 'aarohan',\n",
       " 'aarohi',\n",
       " 'aarolyn',\n",
       " 'aaron',\n",
       " 'aarona',\n",
       " 'aaronae',\n",
       " 'aaronda',\n",
       " 'aaronette',\n",
       " 'aaronisha',\n",
       " 'aaronjacob',\n",
       " 'aaronjames',\n",
       " 'aaronjohn',\n",
       " 'aaronjoseph',\n",
       " 'aaronjosh',\n",
       " 'aaronjoshua',\n",
       " 'aaronlee',\n",
       " 'aaronmichael',\n",
       " 'aaronn',\n",
       " 'aaroosh',\n",
       " 'aarren',\n",
       " 'aarron',\n",
       " 'aarsh',\n",
       " 'aarshi',\n",
       " 'aarshiya',\n",
       " 'aarthi',\n",
       " 'aarti',\n",
       " 'aaruhi',\n",
       " 'aarush',\n",
       " 'aarushi',\n",
       " 'aarvi',\n",
       " 'aarvin',\n",
       " 'aarya',\n",
       " 'aaryah',\n",
       " 'aaryahi',\n",
       " 'aaryan',\n",
       " 'aaryana',\n",
       " 'aaryanna',\n",
       " 'aaryash',\n",
       " 'aaryav',\n",
       " 'aaryn',\n",
       " 'aaryon',\n",
       " 'aaryona',\n",
       " 'aarza',\n",
       " 'aaser',\n",
       " 'aasha',\n",
       " 'aashana',\n",
       " 'aashay',\n",
       " 'aashi',\n",
       " 'aashia',\n",
       " 'aashika',\n",
       " 'aashini',\n",
       " 'aashir',\n",
       " 'aashirya',\n",
       " 'aashish',\n",
       " 'aashita',\n",
       " 'aashiyana',\n",
       " 'aashka',\n",
       " 'aashman',\n",
       " 'aashna',\n",
       " 'aashni',\n",
       " 'aashray',\n",
       " 'aashrita',\n",
       " 'aashrith',\n",
       " 'aashritha',\n",
       " 'aashvi',\n",
       " 'aasia',\n",
       " 'aasim',\n",
       " 'aasin',\n",
       " 'aasir',\n",
       " 'aasiya',\n",
       " 'aasiyah',\n",
       " 'aason',\n",
       " 'aastha',\n",
       " 'aathan',\n",
       " 'aatif',\n",
       " 'aatish',\n",
       " 'aava',\n",
       " 'aavah',\n",
       " 'aavash',\n",
       " 'aaven',\n",
       " 'aavi',\n",
       " 'aavin',\n",
       " 'aavion',\n",
       " 'aavya',\n",
       " 'aavyan',\n",
       " 'aavyn',\n",
       " 'aaya',\n",
       " 'aayaan',\n",
       " 'aayah',\n",
       " 'aayam',\n",
       " 'aayan',\n",
       " 'aayana',\n",
       " 'aayanah',\n",
       " 'aayanna',\n",
       " 'aayansh',\n",
       " 'aayat',\n",
       " 'aaydan',\n",
       " 'aayden',\n",
       " 'aayla',\n",
       " 'aaylah',\n",
       " 'aayliah',\n",
       " 'aayra',\n",
       " 'aayu',\n",
       " 'aayush',\n",
       " 'aayusha',\n",
       " 'aayushi',\n",
       " 'aazan',\n",
       " 'aazim',\n",
       " 'aaziyah',\n",
       " 'ab',\n",
       " 'aba',\n",
       " 'abaan',\n",
       " 'abad',\n",
       " 'abagael',\n",
       " 'abagail',\n",
       " 'abagaile',\n",
       " 'abagale',\n",
       " 'abagayle',\n",
       " 'abaigael',\n",
       " 'abaigail',\n",
       " 'abaigeal',\n",
       " 'aban',\n",
       " 'abanoub',\n",
       " 'abas',\n",
       " 'abasi',\n",
       " 'abass',\n",
       " 'abayomi',\n",
       " 'abb',\n",
       " 'abba',\n",
       " 'abbaas',\n",
       " 'abbagail',\n",
       " 'abbagale',\n",
       " 'abbagayle',\n",
       " 'abbas',\n",
       " 'abbe',\n",
       " 'abbee',\n",
       " 'abbegail',\n",
       " 'abbegale',\n",
       " 'abbegayle',\n",
       " 'abbey',\n",
       " 'abbeygail',\n",
       " 'abbeygale',\n",
       " 'abbi',\n",
       " 'abbie',\n",
       " 'abbiegail',\n",
       " 'abbiegale',\n",
       " 'abbiegayle',\n",
       " 'abbigael',\n",
       " 'abbigail',\n",
       " 'abbigaile',\n",
       " 'abbigal',\n",
       " 'abbigale',\n",
       " 'abbigayl',\n",
       " 'abbigayle',\n",
       " 'abbilyn',\n",
       " 'abbot',\n",
       " 'abbott',\n",
       " 'abbra',\n",
       " 'abbriella',\n",
       " 'abbrielle',\n",
       " 'abby',\n",
       " 'abbye',\n",
       " 'abbygael',\n",
       " 'abbygail',\n",
       " 'abbygaile',\n",
       " 'abbygale',\n",
       " 'abbygayl',\n",
       " 'abbygayle',\n",
       " 'abbylynn',\n",
       " 'abcde',\n",
       " 'abd',\n",
       " 'abdala',\n",
       " 'abdalah',\n",
       " 'abdalla',\n",
       " 'abdallah',\n",
       " 'abdalrahman',\n",
       " 'abdalrhman',\n",
       " 'abdel',\n",
       " 'abdelaziz',\n",
       " 'abdelhadi',\n",
       " 'abdelkareem',\n",
       " 'abdelkarim',\n",
       " 'abdellah',\n",
       " 'abdelrahman',\n",
       " 'abdelrhman',\n",
       " 'abderrahman',\n",
       " 'abdi',\n",
       " 'abdias',\n",
       " 'abdiasis',\n",
       " 'abdiaziz',\n",
       " 'abdiel',\n",
       " 'abdifatah',\n",
       " 'abdihafid',\n",
       " 'abdihakim',\n",
       " 'abdihamid',\n",
       " 'abdikadir',\n",
       " 'abdikarim',\n",
       " 'abdikhaliq',\n",
       " 'abdilahi',\n",
       " 'abdimalik',\n",
       " 'abdinajib',\n",
       " 'abdinasir',\n",
       " 'abdiqani',\n",
       " 'abdirahim',\n",
       " 'abdirahin',\n",
       " 'abdirahman',\n",
       " 'abdirashid',\n",
       " 'abdirisaq',\n",
       " 'abdirizak',\n",
       " 'abdisalam',\n",
       " 'abdisalan',\n",
       " 'abdisamad',\n",
       " 'abdishakur',\n",
       " 'abdiwahab',\n",
       " 'abdiwali',\n",
       " 'abdo',\n",
       " 'abdon',\n",
       " 'abdou',\n",
       " 'abdoul',\n",
       " 'abdoulaye',\n",
       " 'abdoulaziz',\n",
       " 'abdoulie',\n",
       " 'abdourahman',\n",
       " 'abdourahmane',\n",
       " 'abdrahman',\n",
       " 'abdrew',\n",
       " 'abdu',\n",
       " 'abdual',\n",
       " 'abduallah',\n",
       " 'abduel',\n",
       " 'abdul',\n",
       " 'abdula',\n",
       " 'abdulah',\n",
       " 'abdulahad',\n",
       " 'abdulahi',\n",
       " 'abdulai',\n",
       " 'abdulaye',\n",
       " 'abdulazeem',\n",
       " 'abdulazeez',\n",
       " 'abdulaziz',\n",
       " 'abdulbari',\n",
       " 'abdulbasit',\n",
       " 'abdule',\n",
       " 'abdulelah',\n",
       " 'abdulhadi',\n",
       " 'abdulhakeem',\n",
       " 'abdulhakim',\n",
       " 'abdulhalim',\n",
       " 'abdulhameed',\n",
       " 'abdulhamid',\n",
       " 'abduljabbar',\n",
       " 'abduljaleel',\n",
       " 'abduljalil',\n",
       " 'abdulkadir',\n",
       " 'abdulkareem',\n",
       " 'abdulkarim',\n",
       " 'abdulkhaliq',\n",
       " 'abdull',\n",
       " 'abdulla',\n",
       " 'abdullah',\n",
       " 'abdullahi',\n",
       " 'abdullatif',\n",
       " 'abdulloh',\n",
       " 'abdulmajeed',\n",
       " 'abdulmajid',\n",
       " 'abdulmalek',\n",
       " 'abdulmalik',\n",
       " 'abdulmohsen',\n",
       " 'abdulnasir',\n",
       " 'abdulqadir',\n",
       " 'abdulraheem',\n",
       " 'abdulrahim',\n",
       " 'abdulrahman',\n",
       " 'abdulrazaq',\n",
       " 'abdulrehman',\n",
       " 'abdulrhman',\n",
       " 'abdulsalam',\n",
       " 'abdulsamad',\n",
       " 'abdulwadud',\n",
       " 'abdulwahab',\n",
       " 'abdulwahid',\n",
       " 'abdur',\n",
       " 'abdurahman',\n",
       " 'abdurahmon',\n",
       " 'abdurraheem',\n",
       " 'abdurrahim',\n",
       " 'abdurrahmaan',\n",
       " 'abdurrahman',\n",
       " 'abdurrehman',\n",
       " 'abdussamad',\n",
       " 'abe',\n",
       " 'abeal',\n",
       " 'abed',\n",
       " 'abedallah',\n",
       " 'abedalrahman',\n",
       " 'abednego',\n",
       " 'abeeha',\n",
       " 'abeer',\n",
       " 'abeera',\n",
       " 'abegail',\n",
       " 'abegale',\n",
       " 'abegayle',\n",
       " 'abel',\n",
       " 'abela',\n",
       " 'abelardo',\n",
       " 'abelina',\n",
       " 'abelino',\n",
       " 'abell',\n",
       " 'abella',\n",
       " 'abem',\n",
       " 'aben',\n",
       " 'abena',\n",
       " 'abenezer',\n",
       " 'abeni',\n",
       " 'aber',\n",
       " 'aberdeen',\n",
       " 'aberham',\n",
       " 'abernathy',\n",
       " 'abert',\n",
       " 'abery',\n",
       " 'abey',\n",
       " 'abgail',\n",
       " 'abha',\n",
       " 'abhay',\n",
       " 'abheek',\n",
       " 'abhi',\n",
       " 'abhigna',\n",
       " 'abhijay',\n",
       " 'abhijeet',\n",
       " 'abhijit',\n",
       " 'abhijot',\n",
       " 'abhik',\n",
       " 'abhilash',\n",
       " 'abhimanyu',\n",
       " 'abhinav',\n",
       " 'abhinay',\n",
       " 'abhinaya',\n",
       " 'abhiraam',\n",
       " 'abhiraj',\n",
       " 'abhiram',\n",
       " 'abhirup',\n",
       " 'abhishek',\n",
       " 'abhyuday',\n",
       " 'abi',\n",
       " 'abia',\n",
       " 'abiageal',\n",
       " 'abiah',\n",
       " 'abian',\n",
       " 'abianna',\n",
       " 'abibail',\n",
       " 'abid',\n",
       " 'abida',\n",
       " 'abidah',\n",
       " 'abidan',\n",
       " 'abie',\n",
       " 'abiegail',\n",
       " 'abiel',\n",
       " 'abiela',\n",
       " 'abiella',\n",
       " 'abiezer',\n",
       " 'abigael',\n",
       " 'abigaelle',\n",
       " 'abigahil',\n",
       " 'abigai',\n",
       " 'abigail',\n",
       " 'abigaile',\n",
       " 'abigailgrace',\n",
       " 'abigaille',\n",
       " 'abigailmarie',\n",
       " 'abigailrose',\n",
       " 'abigal',\n",
       " 'abigale',\n",
       " 'abigayil',\n",
       " 'abigayl',\n",
       " 'abigayle',\n",
       " 'abigeal',\n",
       " 'abigel',\n",
       " 'abigial',\n",
       " 'abiha',\n",
       " 'abihail',\n",
       " 'abijah',\n",
       " 'abilene',\n",
       " 'abilgail',\n",
       " 'abilio',\n",
       " 'abilyn',\n",
       " 'abilynn',\n",
       " 'abimael',\n",
       " 'abimbola',\n",
       " 'abimelec',\n",
       " 'abin',\n",
       " 'abinadab',\n",
       " 'abinadi',\n",
       " 'abinav',\n",
       " 'abinaya',\n",
       " 'abiodun',\n",
       " 'abiola',\n",
       " 'abiona',\n",
       " 'abir',\n",
       " 'abira',\n",
       " 'abiram',\n",
       " 'abirami',\n",
       " 'abisag',\n",
       " 'abisai',\n",
       " 'abish',\n",
       " 'abisha',\n",
       " 'abishai',\n",
       " 'abishek',\n",
       " 'abisola',\n",
       " 'abiud',\n",
       " 'abiyah',\n",
       " 'abla',\n",
       " 'able',\n",
       " 'abnel',\n",
       " 'abner',\n",
       " 'abney',\n",
       " 'abony',\n",
       " 'abou',\n",
       " 'aboubacar',\n",
       " 'aboubakar',\n",
       " 'abra',\n",
       " 'abraam',\n",
       " 'abraar',\n",
       " 'abrah',\n",
       " 'abraham',\n",
       " 'abrahan',\n",
       " 'abraheem',\n",
       " 'abrahem',\n",
       " 'abrahim',\n",
       " 'abrahm',\n",
       " 'abram',\n",
       " 'abran',\n",
       " 'abranda',\n",
       " 'abrar',\n",
       " 'abraxas',\n",
       " 'abrea',\n",
       " 'abreana',\n",
       " 'abreanna',\n",
       " 'abree',\n",
       " 'abreia',\n",
       " 'abren',\n",
       " 'abreona',\n",
       " 'abreonna',\n",
       " 'abrey',\n",
       " 'abreya',\n",
       " 'abrham',\n",
       " 'abri',\n",
       " 'abria',\n",
       " 'abriah',\n",
       " 'abrial',\n",
       " 'abriam',\n",
       " 'abrian',\n",
       " 'abriana',\n",
       " 'abrianna',\n",
       " 'abriannah',\n",
       " 'abrianne',\n",
       " 'abrie',\n",
       " 'abriel',\n",
       " 'abriela',\n",
       " 'abriele',\n",
       " 'abriella',\n",
       " 'abrielle',\n",
       " 'abrien',\n",
       " 'abrienne',\n",
       " 'abrigail',\n",
       " 'abrihet',\n",
       " 'abril',\n",
       " 'abrille',\n",
       " 'abrina',\n",
       " 'abrion',\n",
       " 'abriona',\n",
       " 'abrionna',\n",
       " 'abrish',\n",
       " 'abriya',\n",
       " 'abriyah',\n",
       " 'abriyana',\n",
       " 'abrom',\n",
       " 'abron',\n",
       " 'abrum',\n",
       " 'abry',\n",
       " 'abryana',\n",
       " 'abryanna',\n",
       " 'abryella',\n",
       " 'abryelle',\n",
       " 'abryl',\n",
       " 'absalat',\n",
       " 'absalom',\n",
       " 'absalon',\n",
       " 'abshir',\n",
       " 'absidy',\n",
       " 'abtin',\n",
       " 'abu',\n",
       " 'abubacar',\n",
       " 'abubacarr',\n",
       " 'abubakar',\n",
       " 'abubakarr',\n",
       " 'abubakary',\n",
       " 'abubaker',\n",
       " 'abubakr',\n",
       " 'abuk',\n",
       " 'abukar',\n",
       " 'abundio',\n",
       " 'aby',\n",
       " 'abyade',\n",
       " 'abyan',\n",
       " 'abygael',\n",
       " 'abygail',\n",
       " 'abygaile',\n",
       " 'abygale',\n",
       " 'abygayle',\n",
       " 'abyssinia',\n",
       " 'ac',\n",
       " 'acacia',\n",
       " 'acacius',\n",
       " 'acadia',\n",
       " 'acamas',\n",
       " 'acari',\n",
       " 'acasia',\n",
       " 'accacia',\n",
       " 'accalia',\n",
       " 'access',\n",
       " 'accie',\n",
       " 'accursio',\n",
       " 'ace',\n",
       " 'acea',\n",
       " 'acein',\n",
       " 'acel',\n",
       " 'acelin',\n",
       " 'acelino',\n",
       " 'acelyn',\n",
       " 'acelynn',\n",
       " 'acen',\n",
       " 'acencion',\n",
       " 'aceon',\n",
       " 'acer',\n",
       " 'aceson',\n",
       " 'acesyn',\n",
       " 'aceton',\n",
       " 'acey',\n",
       " 'aceyn',\n",
       " 'achai',\n",
       " 'achaia',\n",
       " 'achan',\n",
       " 'achante',\n",
       " 'achanti',\n",
       " 'achary',\n",
       " 'achazia',\n",
       " 'achel',\n",
       " 'acheron',\n",
       " 'achille',\n",
       " 'achilles',\n",
       " 'achilleus',\n",
       " 'achillies',\n",
       " 'achintya',\n",
       " 'achol',\n",
       " 'achraf',\n",
       " 'achsa',\n",
       " 'achsah',\n",
       " 'achyut',\n",
       " 'achyuth',\n",
       " 'acia',\n",
       " 'aciano',\n",
       " 'acie',\n",
       " 'aciel',\n",
       " 'acil',\n",
       " 'acire',\n",
       " 'ackeem',\n",
       " 'ackley',\n",
       " 'acob',\n",
       " 'acquanetta',\n",
       " 'acquanette',\n",
       " 'acsa',\n",
       " 'acura',\n",
       " 'acxel',\n",
       " 'acy',\n",
       " 'ad',\n",
       " 'ada',\n",
       " 'adabel',\n",
       " 'adabella',\n",
       " 'adabelle',\n",
       " 'adacia',\n",
       " 'adae',\n",
       " 'adael',\n",
       " 'adaelyn',\n",
       " 'adaeze',\n",
       " 'adagio',\n",
       " 'adah',\n",
       " 'adahir',\n",
       " 'adahli',\n",
       " 'adahlia',\n",
       " 'adahy',\n",
       " 'adai',\n",
       " 'adaia',\n",
       " 'adaiah',\n",
       " 'adaija',\n",
       " 'adaijah',\n",
       " 'adailyn',\n",
       " 'adain',\n",
       " 'adair',\n",
       " 'adaira',\n",
       " 'adaire',\n",
       " 'adairis',\n",
       " 'adaisha',\n",
       " 'adaisia',\n",
       " 'adaja',\n",
       " 'adajah',\n",
       " 'adaku',\n",
       " 'adal',\n",
       " 'adala',\n",
       " 'adalade',\n",
       " 'adalae',\n",
       " 'adalai',\n",
       " 'adalaide',\n",
       " 'adalay',\n",
       " 'adalaya',\n",
       " 'adalayde',\n",
       " 'adalbert',\n",
       " 'adalberto',\n",
       " 'adale',\n",
       " 'adalea',\n",
       " 'adaleah',\n",
       " 'adalee',\n",
       " 'adaleen',\n",
       " 'adaleena',\n",
       " 'adalei',\n",
       " 'adaleia',\n",
       " 'adaleigh',\n",
       " 'adaleine',\n",
       " 'adalen',\n",
       " 'adalena',\n",
       " 'adalene',\n",
       " 'adaley',\n",
       " 'adaleya',\n",
       " 'adaleyza',\n",
       " 'adalhi',\n",
       " 'adali',\n",
       " 'adalia',\n",
       " 'adaliah',\n",
       " 'adalicia',\n",
       " 'adalid',\n",
       " 'adalida',\n",
       " 'adalie',\n",
       " 'adaliene',\n",
       " 'adalin',\n",
       " 'adalina',\n",
       " 'adalind',\n",
       " 'adalinda',\n",
       " 'adaline',\n",
       " 'adalinn',\n",
       " 'adalinne',\n",
       " 'adalis',\n",
       " 'adalisa',\n",
       " 'adalise',\n",
       " 'adalisse',\n",
       " 'adalius',\n",
       " 'adaliyah',\n",
       " 'adaliz',\n",
       " 'adalize',\n",
       " 'adallyn',\n",
       " 'adaly',\n",
       " 'adalya',\n",
       " 'adalye',\n",
       " 'adalyn',\n",
       " 'adalyna',\n",
       " 'adalynd',\n",
       " 'adalyne',\n",
       " 'adalynn',\n",
       " 'adalynne',\n",
       " 'adalys',\n",
       " 'adalyse',\n",
       " 'adam',\n",
       " 'adama',\n",
       " 'adamae',\n",
       " 'adamari',\n",
       " 'adamarie',\n",
       " 'adamaris',\n",
       " 'adamariz',\n",
       " 'adamary',\n",
       " 'adamarys',\n",
       " 'adamina',\n",
       " 'adamm',\n",
       " 'adamma',\n",
       " 'adammichael',\n",
       " 'adamo',\n",
       " 'adams',\n",
       " 'adan',\n",
       " 'adana',\n",
       " 'adaneli',\n",
       " 'adanelly',\n",
       " 'adanely',\n",
       " 'adanna',\n",
       " 'adannaya',\n",
       " 'adante',\n",
       " 'adanya',\n",
       " 'adaobi',\n",
       " 'adaora',\n",
       " 'adar',\n",
       " 'adara',\n",
       " 'adarah',\n",
       " 'adari',\n",
       " 'adaria',\n",
       " 'adarian',\n",
       " 'adarien',\n",
       " 'adarion',\n",
       " 'adarious',\n",
       " 'adarius',\n",
       " 'adarrius',\n",
       " 'adarryl',\n",
       " 'adarryll',\n",
       " 'adarsh',\n",
       " 'adaryl',\n",
       " 'adaryll',\n",
       " 'adasha',\n",
       " 'adashia',\n",
       " 'adasia',\n",
       " 'adason',\n",
       " 'adassa',\n",
       " 'adasyn',\n",
       " 'adaugo',\n",
       " 'adaure',\n",
       " 'adavia',\n",
       " 'adavion',\n",
       " 'adaya',\n",
       " 'adayah',\n",
       " 'adayla',\n",
       " 'adaysha',\n",
       " 'adayshia',\n",
       " 'adaysia',\n",
       " 'adbeel',\n",
       " 'adbiel',\n",
       " 'add',\n",
       " 'adda',\n",
       " 'addah',\n",
       " 'addai',\n",
       " 'addalee',\n",
       " 'addaleigh',\n",
       " 'addaley',\n",
       " 'addalie',\n",
       " 'addalin',\n",
       " 'addalina',\n",
       " 'addaline',\n",
       " 'addalyn',\n",
       " 'addalyne',\n",
       " 'addalynn',\n",
       " 'addalynne',\n",
       " 'addam',\n",
       " 'addan',\n",
       " ...]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cv.get_feature_names()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Features \n",
    "X\n",
    "# Labels\n",
    "y = df_names.sex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6398163206734908"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Naive Bayes Classifier\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "clf = MultinomialNB()\n",
    "clf.fit(X_train,y_train)\n",
    "clf.score(X_test,y_test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy of Model 63.98163206734908 %\n"
     ]
    }
   ],
   "source": [
    "# Accuracy of our Model\n",
    "print(\"Accuracy of Model\",clf.score(X_test,y_test)*100,\"%\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy of Model 100.0 %\n"
     ]
    }
   ],
   "source": [
    "# Accuracy of our Model\n",
    "print(\"Accuracy of Model\",clf.score(X_train,y_train)*100,\"%\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sample Prediction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample1 Prediction\n",
    "sample_name = [\"Mary\"]\n",
    "vect = cv.transform(sample_name).toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, ..., 0, 0, 0]], dtype=int64)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0], dtype=int64)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Female is 0, Male is 1\n",
    "clf.predict(vect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample2 Prediction\n",
    "sample_name1 = [\"Mark\"]\n",
    "vect1 = cv.transform(sample_name1).toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1], dtype=int64)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf.predict(vect1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample3 Prediction of Russian Names\n",
    "sample_name2 = [\"Natasha\"]\n",
    "vect2 = cv.transform(sample_name2).toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0], dtype=int64)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf.predict(vect2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample3 Prediction of Random Names\n",
    "sample_name3 = [\"Nefertiti\",\"Nasha\",\"Ama\",\"Ayo\",\"Xhavier\",\"Ovetta\",\"Tathiana\",\"Xia\",\"Joseph\",\"Xianliang\"]\n",
    "vect3 = cv.transform(sample_name3).toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0], dtype=int64)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf.predict(vect3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# A function to do it\n",
    "def genderpredictor(a):\n",
    "    test_name = [a]\n",
    "    vector = cv.transform(test_name).toarray()\n",
    "    if clf.predict(vector) == 0:\n",
    "        print(\"Female\")\n",
    "    else:\n",
    "        print(\"Male\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Female\n"
     ]
    }
   ],
   "source": [
    "genderpredictor(\"Martha\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Features fxn\n",
    "apply the fxn\n",
    "vectorizer\n",
    "fit\n",
    "transform\n",
    "classifier\n",
    "fit\n",
    "predict\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Female\n",
      "None\n",
      "Male\n",
      "None\n",
      "Female\n",
      "None\n",
      "Female\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "namelist = [\"Yaa\",\"Yaw\",\"Femi\",\"Masha\"]\n",
    "for i in namelist:\n",
    "    print(genderpredictor(i))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Using a custom function for feature analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "# By Analogy most female names ends in 'A' or 'E' or has the sound of 'A'\n",
    "def features(name):\n",
    "    name = name.lower()\n",
    "    return {\n",
    "        'first-letter': name[0], # First letter\n",
    "        'first2-letters': name[0:2], # First 2 letters\n",
    "        'first3-letters': name[0:3], # First 3 letters\n",
    "        'last-letter': name[-1],\n",
    "        'last2-letters': name[-2:],\n",
    "        'last3-letters': name[-3:],\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'first-letter': 'a', 'first2-letters': 'an', 'first3-letters': 'ann', 'last-letter': 'a', 'last2-letters': 'na', 'last3-letters': 'nna'}\n",
      " {'first-letter': 'h', 'first2-letters': 'ha', 'first3-letters': 'han', 'last-letter': 'h', 'last2-letters': 'ah', 'last3-letters': 'nah'}\n",
      " {'first-letter': 'p', 'first2-letters': 'pe', 'first3-letters': 'pet', 'last-letter': 'r', 'last2-letters': 'er', 'last3-letters': 'ter'}\n",
      " {'first-letter': 'j', 'first2-letters': 'jo', 'first3-letters': 'joh', 'last-letter': 'n', 'last2-letters': 'hn', 'last3-letters': 'ohn'}\n",
      " {'first-letter': 'v', 'first2-letters': 'vl', 'first3-letters': 'vla', 'last-letter': 'r', 'last2-letters': 'ir', 'last3-letters': 'mir'}\n",
      " {'first-letter': 'm', 'first2-letters': 'mo', 'first3-letters': 'moh', 'last-letter': 'd', 'last2-letters': 'ed', 'last3-letters': 'med'}]\n"
     ]
    }
   ],
   "source": [
    "# Vectorize the features function\n",
    "features = np.vectorize(features)\n",
    "print(features([\"Anna\", \"Hannah\", \"Peter\",\"John\",\"Vladmir\",\"Mohammed\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Extract the features for the dataset\n",
    "df_X = features(df_names['name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_y = df_names['sex']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  (0, 1)\t1.0\n",
      "  (0, 3)\t1.0\n",
      "  (0, 5)\t1.0\n",
      "  (0, 7)\t1.0\n",
      "  (0, 9)\t1.0\n",
      "  (0, 10)\t1.0\n",
      "  (1, 0)\t1.0\n",
      "  (1, 2)\t1.0\n",
      "  (1, 4)\t1.0\n",
      "  (1, 6)\t1.0\n",
      "  (1, 8)\t1.0\n",
      "  (1, 11)\t1.0\n"
     ]
    }
   ],
   "source": [
    "from sklearn.feature_extraction import DictVectorizer\n",
    " \n",
    "corpus = features([\"Mike\", \"Julia\"])\n",
    "dv = DictVectorizer()\n",
    "dv.fit(corpus)\n",
    "transformed = dv.transform(corpus)\n",
    "print(transformed)\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['first-letter=j',\n",
       " 'first-letter=m',\n",
       " 'first2-letters=ju',\n",
       " 'first2-letters=mi',\n",
       " 'first3-letters=jul',\n",
       " 'first3-letters=mik',\n",
       " 'last-letter=a',\n",
       " 'last-letter=e',\n",
       " 'last2-letters=ia',\n",
       " 'last2-letters=ke',\n",
       " 'last3-letters=ike',\n",
       " 'last3-letters=lia']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dv.get_feature_names()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Train Test Split\n",
    "dfX_train, dfX_test, dfy_train, dfy_test = train_test_split(df_X, df_y, test_size=0.33, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([{'first-letter': 'e', 'first2-letters': 'el', 'first3-letters': 'ele', 'last-letter': 'a', 'last2-letters': 'ia', 'last3-letters': 'nia'},\n",
       "       {'first-letter': 'a', 'first2-letters': 'ad', 'first3-letters': 'adi', 'last-letter': 'l', 'last2-letters': 'il', 'last3-letters': 'dil'},\n",
       "       {'first-letter': 'k', 'first2-letters': 'ka', 'first3-letters': 'kad', 'last-letter': 'e', 'last2-letters': 'ze', 'last3-letters': 'nze'},\n",
       "       ...,\n",
       "       {'first-letter': 'j', 'first2-letters': 'ja', 'first3-letters': 'jaz', 'last-letter': 'y', 'last2-letters': 'ly', 'last3-letters': 'zly'},\n",
       "       {'first-letter': 'e', 'first2-letters': 'el', 'first3-letters': 'elv', 'last-letter': 'a', 'last2-letters': 'na', 'last3-letters': 'ina'},\n",
       "       {'first-letter': 'l', 'first2-letters': 'le', 'first3-letters': 'led', 'last-letter': 'r', 'last2-letters': 'er', 'last3-letters': 'ger'}],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfX_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<63666x8194 sparse matrix of type '<class 'numpy.float64'>'\n",
       "\twith 381996 stored elements in Compressed Sparse Row format>"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "dv = DictVectorizer()\n",
    "dv.fit_transform(dfX_train)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,\n",
       "            max_features=None, max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=1, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, presort=False, random_state=None,\n",
       "            splitter='best')"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Model building Using DecisionTree\n",
    "\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    " \n",
    "dclf = DecisionTreeClassifier()\n",
    "my_xfeatures =dv.transform(dfX_train)\n",
    "dclf.fit(my_xfeatures, dfy_train)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Build Features and Transform them\n",
    "sample_name_eg = [\"Alex\"]\n",
    "transform_dv =dv.transform(features(sample_name_eg))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "vect3 = transform_dv.toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1], dtype=int64)"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Predicting Gender of Name\n",
    "# Male is 1,female = 0\n",
    "dclf.predict(vect3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Male\n"
     ]
    }
   ],
   "source": [
    "if dclf.predict(vect3) == 0:\n",
    "    print(\"Female\")\n",
    "else:\n",
    "    print(\"Male\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Female\n"
     ]
    }
   ],
   "source": [
    "# Second Prediction With Nigerian Name\n",
    "name_eg1 = [\"Chioma\"]\n",
    "transform_dv =dv.transform(features(name_eg1))\n",
    "vect4 = transform_dv.toarray()\n",
    "if dclf.predict(vect4) == 0:\n",
    "    print(\"Female\")\n",
    "else:\n",
    "    print(\"Male\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# A function to do it\n",
    "def genderpredictor1(a):\n",
    "    test_name1 = [a]\n",
    "    transform_dv =dv.transform(features(test_name1))\n",
    "    vector = transform_dv.toarray()\n",
    "    if dclf.predict(vector) == 0:\n",
    "        print(\"Female\")\n",
    "    else:\n",
    "        print(\"Male\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "random_name_list = [\"Alex\",\"Alice\",\"Chioma\",\"Vitalic\",\"Clairese\",\"Chan\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Male\n",
      "None\n",
      "Female\n",
      "None\n",
      "Female\n",
      "None\n",
      "Female\n",
      "None\n",
      "Female\n",
      "None\n",
      "Male\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "for n in random_name_list:\n",
    "    print(genderpredictor1(n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9888951716771903\n"
     ]
    }
   ],
   "source": [
    "## Accuracy of Models Decision Tree Classifier Works better than Naive Bayes\n",
    "# Accuracy on training set\n",
    "print(dclf.score(dv.transform(dfX_train), dfy_train)) \n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.8682355942472655\n"
     ]
    }
   ],
   "source": [
    "# Accuracy on test set\n",
    "print(dclf.score(dv.transform(dfX_test), dfy_test))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Saving Our Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.externals import joblib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "decisiontreModel = open(\"decisiontreemodel.pkl\",\"wb\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "joblib.dump(dclf,decisiontreModel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function BufferedWriter.close>"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "decisiontreModel.close"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Alternative to Model Saving\n",
    "import pickle\n",
    "dctreeModel = open(\"namesdetectormodel.pkl\",\"wb\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pickle.dump(dclf,dctreeModel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dctreeModel.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Save Multinomial NB Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "NaiveBayesModel = open(\"naivebayesgendermodel.pkl\",\"wb\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "joblib.dump(clf,NaiveBayesModel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "NaiveBayesModel.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Thanks\n",
    "# By Jesse JCharis\n",
    "# Jesus Saves @ JCharisTech\n",
    "# J-Secur1ty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
